package com.example.patrick.readtxtfile;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;


public class MyActivity extends Activity {

    private static final String LOG_TAG = "" ;
    static Vibrator myVibrator;
    private static int counter = 0;
    private static int checkbox_count = 0;
    private static LinearLayout bgElement;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.example.patrick.readtxtfile.R.layout.activity_my);
        bgElement = (LinearLayout) findViewById(com.example.patrick.readtxtfile.R.id.MyLayout);
          /* Init Background to Red */
        bgElement.setBackgroundColor(Color.WHITE);
        counter = 0;

        /* Get File */
        File myFile = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES),"Hallo.txt");

        /* Read Text File */
        TextFileIO myTextFileParser = new TextFileIO();
        int CheckBoxCount = 0;
        ArrayList<String> pArray = new ArrayList<String>();
        myTextFileParser.readTextFile(myFile,pArray);
        //myTextFileParser.readTextFile(myFile);
        checkbox_count = pArray.size();

        ArrayList<CheckBox> CheckBoxArray = new ArrayList<CheckBox>();

        for (int i = 0; i < pArray.size(); i++) {
            CheckBoxArray.add(new CheckBox(this));
            /* Set Checkbox Action Listener */
            CheckBoxArray.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean checked = ((CheckBox) v).isChecked();
                    if(checked) {
                        counter++;
                    }else
                    {
                        counter--;
                    }
                    // Check which checkbox was clicked
                    LinearLayout bgElement = (LinearLayout) findViewById(com.example.patrick.readtxtfile.R.id.MyLayout);
                    if(counter == checkbox_count)
                    {
                    /* Change Background to Green to pass Childs to door */
                        bgElement.setBackgroundColor(Color.GREEN);
                    }else
                    {
                    /* Change Background to red */
                        bgElement.setBackgroundColor(Color.rgb(65000,2000,2000));
                    }
                }
            });

            /* Set Checkbox-Name to ArrayName */
            CheckBoxArray.get(i).setText(pArray.get(i));
            bgElement.addView(CheckBoxArray.get(i));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(com.example.patrick.readtxtfile.R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == com.example.patrick.readtxtfile.R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
